/**
 * 广告详情
 */
define([ '../../common/service'], function( http) {
	var AdDetail = {
		firstShowLoading: true,
		init: function() {
			var _this = this;
			var adId =Kit.getUrlParam("id");
			if(!adId || adId == '' || !Number(adId)){
				Kit.alert2(I18N.getText('adDetail','errorparams'));
				return false;
			}
			_this.initPage(adId);
		},
		initPage: function(adId) {
			var _this = this;
			_this._getData(adId);
			_this._getDataSIV = setInterval(function() {
				_this._getData(adId);
			}, 5000);
		},
		/*
		 @获取数据
		 * */
		_getData: function(adId) {
			var _this = this;
			if (_this.firstShowLoading) {
				Kit.loading.show($('.container-fluid'));
			}
			var par = {id:adId};
            http.work('/publishbatchs/searchPublishBatchDetailInfoById',par,'POST').then(function(res){
				_this.renderPage(res.data);
				if (_this.firstShowLoading) {
					Kit.loading.hide($('.container-fluid'));
				}
				_this.firstShowLoading = false;
			});
		}
		/*
		 @渲染页面
		 * */
		,renderPage: function(data) {
			var _this = this;
			//渲染发布状态区域HTML
			var publishStatusArea = $('#publishStatus');
			publishStatusArea.html( _this.createStatusHtml(data) );

			//渲染账号列表区域HTML
			var adAccount = $('#ad-account');
			$('tbody', adAccount).html( _this.createAccountListHtml(data) );



			//渲染错误信息区域HTML
			var adFailPanel = $('#ad-fail-panel');
			if(_this.isShowFailPanel(data)){
				adFailPanel.html( _this.createFailHtml(data) );

				adAccount.addClass('col-md-6').removeClass('col-md-12');
				adFailPanel.show();
			}else {
				adAccount.addClass('col-md-12').removeClass('col-md-6');
				adFailPanel.hide();
			}
			adAccount.show();
			if (data.publishBatchDetail.publishStatus * 1 != 1) {
				clearInterval(_this._getDataSIV);
			}
		}
		/*
		  @生成状态区域HTML
		 * */
		,createStatusHtml: function(data){
			var publishStatus = (function(){
				if(data.publishBatchDetail.publishStatus==0 || data.publishBatchDetail.publishStatus==4 || data.publishBatchDetail.publishStatus==10){
					return I18N.getText('adDetail','waitpublish');
				} else if(data.publishBatchDetail.publishStatus==1 || data.publishBatchDetail.publishStatus==11 ){
					return I18N.getText('adDetail','publishing');
				} else if(data.publishBatchDetail.publishStatus==2){
					return '<span style="color:green">'+I18N.getText('adDetail','publishsucceed')+'</span>'
				} else if(data.publishBatchDetail.publishStatus==3){
					return '<span style="color:red">'+I18N.getText('adDetail','publishfail')+'</span>'
				} else if(data.publishBatchDetail.publishStatus==5){
					return I18N.getText('adDetail','redo')
				}
			})();
			var _html = '<span class="mr10">'+I18N.getText('adDetail','name')+': '
						   + data.publishBatchDetail.publishBatchName
						   + '</span>'
						   + '<span>'
						   + publishStatus
						   + '</span>';
			return _html;
		}
		/*
		 	@生成账号列表 区域HTML
		 * */
		,createAccountListHtml: function(data){
			var _html = '',
				redClass ='';
			 $.each(data.publishBatchDetail.pulishBatchEachAccountInfos, function(i,v) {
			 	redClass = v.failCount > 0 ? ' red' : '';
				_html += '<tr>'
					  + '<td class="text-center">'
					  + v.accountName
					  + '</td>'
					  + '<td class="text-center'+redClass+'">'
					  + v.compaignGroupCount +'/'+ v.compaignGroupAll
					  + '</td>'
					  + '<td class="text-center'+redClass+'">'
					  + v.adSetCount +'/'+ v.adSetCountAll
					  + '</td>'
					  + '<td class="text-center'+redClass+'">'
					  + v.adGroupCount +'/'+ v.adGroupCountAll
					  + '</td>'
					  + '<td class="text-center'+redClass+'">'
					  + v.adCreativeCount +'/'+ v.adCreativeCountAll
					  + '</td>'
					  + '</tr>';
	 		});
	 		return _html;
		}
		/*
		 @生成失败信息区域HTML
		 * */
		,createFailHtml: function(data){
			var _html = '';
			$.each(data.publishBatchDetail.pulishBatchEachAccountInfos, function(i, acc){
                if (acc.failCount) {
                    for (var j = 0; j < acc.publishResultFailDetails.length; j++) {
                        var failDetail = acc.publishResultFailDetails[j];
                        if(failDetail.exception && (failDetail.exception.indexOf('</body>')>0)){
                            var exception = $(failDetail.exception);
                            var result = '<br>';
                            $(exception.children()).each(function(i,v){
                                if(this.outerHTML.indexOf('header')>0){
                                    return;
                                }
                                if(this.outerHTML.indexOf('back-link')>0){
                                    return;
                                }
                                if(this.outerHTML.indexOf('footer')>0){
                                    return;
                                }
                                result =result + this.innerHTML+"<br>";
                            });
                            failDetail.exception = result;
                        }
                    }
                }
                _html += '<div class="panel panel-default" id="fail-panel-'+acc.fbAccountId+'">'
  					  + '<div class="panel-heading">'
  					  + '<h4 class="panel-title">'
  					  + '<a data-toggle="collapse" data-parent="#ad-result" href="#collapse-'+acc.fbAccountId+'" class="collapsed">'
  					  + '<i class="fa fa-fw fa-plus-circle txt-color-green"></i>'
  					  + '<i class="fa fa-fw fa-minus-circle txt-color-red"></i>'
  					  + acc.accountName
  					  + '<span class="pull-right fail-count">'+I18N.getText('adDetail','failNo')+':'
  					  + '<span style="color:red">'
  					  + acc.failCount
  					  + '</span>'
  					  + '</span>'
  					  + '</a>'
  					  + '</h4>'
  					  + '</div>'
  					  + '<div id="collapse-'+ acc.fbAccountId+'" class="panel-collapse collapse" style="height: 0px;">'
  					  + '<div class="panel-body pre-scrollable">';
  				if(acc.publishResultFailDetails && acc.publishResultFailDetails.length > 0){
  					_html += '<ul class="list">'
		  			$.each(acc.publishResultFailDetails, function(i, result) {
						_html +=  '<li style="line-height: 24px">'
							  + '<span style="color:red">' + result.result + '</span>&nbsp;&nbsp;';
							  try{
								  _html += JSON.stringify($.parseJSON(result.exception));
							  } catch (e) {
								  _html += result.exception;
							  }
						_html += '</li>';
					});
		  			_html += '</ul>';
  				}
	  			_html +='</div>'
	  				  + '</div>'
	  				  + '</div>';
			});
			return _html;
		}
		/*
		 @判断是否显示失败信息区域
		 * */
		,isShowFailPanel: function(data) {
			var _this = this;
			var isShow = false;
			var accInfos = data.publishBatchDetail.pulishBatchEachAccountInfos;

			for (var i = 0; i < accInfos.length; i++) {
				var acc = accInfos[i];
				if (acc.failCount) {
					isShow = true;
					break;
				}
			}
			return isShow;
		}
	}
	return AdDetail;
});
